#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b; i-- > a;)
#define sz(x) (int)(x).size()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define fastio               \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << endl
#else
#define debug(x)
#endif
const int N = 2e5 + 10;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
const int MAXN = 1e6 + 5;

template <typename T>
T gcd(T a, T b)
{
    return b ? gcd(b, a % b) : a;
}
template <typename T>
T lcm(T a, T b)
{
    return a / gcd(a, b) * b;
}
// 快速幂
ll powmod(ll base, ll exp, ll mod)
{
    ll result = 1;
    while (exp > 0)
    {
        if (exp % 2 == 1)
            result = result * base % mod;
        base = base * base % mod;
        exp /= 2;
    }
    return result;
}

int vis[N];
// 计算组合数 C(n, k) % MOD
ll fact[MAXN], ifact[MAXN]; // 存储阶乘及其逆元

void init_combinatorics(int n, int mod)
{
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
    {
        fact[i] = fact[i - 1] * i % mod;
    }
    ifact[n] = powmod(fact[n], mod - 2, mod); // 计算阶乘逆元，使用费马小定理
    for (int i = n - 1; i >= 0; --i)
    {
        ifact[i] = ifact[i + 1] * (i + 1) % mod;
    }
}

ll comb(int n, int k, int mod)
{
    if (k > n || k < 0)
        return 0;
    return fact[n] * ifact[k] % mod * ifact[n - k] % mod;
}
// 二分查找
template <typename T>
int binary_search(const vector<T> &arr, T target)
{
    int low = 0, high = sz(arr) - 1;
    while (low <= high)
    {
        int mid = (low + high) / 2;
        if (arr[mid] == target)
            return mid;
        else if (arr[mid] < target)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return -1;
}
// 并查集
struct DSU
{
    vi parent, rank;
    DSU(int n) : parent(n), rank(n, 1)
    {
        iota(all(parent), 0);
    }
    int find(int x)
    {
        return x == parent[x] ? x : (parent[x] = find(parent[x]));
    }
    bool unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            if (rank[x] < rank[y])
                swap(x, y);
            parent[y] = x;
            if (rank[x] == rank[y])
                rank[x]++;
            return true;
        }
        return false;
    }
};
int cc(int a, int b)
{

    int res = 1;
    for (int i = 1; i <= b; i++)
    {
        res *= a - i + 1;
    }
    for (int i = 1; i <= b; i++)
    {
        res /= i;
    }
    return res;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
ll n, m;
int a[110][110];
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            bool f = false;
            int res = 0;
            for (int k = 0; k < 4; k++)
            {
                int x = dx[k] + i, y = dy[k] + j;
                if (x >= 1 && x <= n && y >= 1 && y <= m)
                {
                    if (a[x][y] > a[i][j])
                    {
                        f = true;
                    }
                    res = max(res, a[x][y]);
                }
            }
            if (f)
            {
                continue;
            }
            else
                a[i][j] = res;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << "\n";
    }
}
int main()
{
    fastio;
    ll _ = 1;
    cin >> _;
    while (_--)
        solve();
    return 0;
}